package 常用类.Array类.比较器;

import java.util.Comparator;

/**
 * java.lang.Comparable:该接口只能定义一套排序规则，一般用于定义对象的主排序规则
 * java.util.Comparator:该接口可以定义多套排序规则，根据需要从其中选择需要的排序规则
 */
public class User implements Comparable<User>{
    private Integer id;
    private String name;
    private Integer age;
    private Integer height;//身高

    public User() {
    }

    public User(Integer id, String name, Integer age, Integer height) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.height = height;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Integer getHeight() {
        return height;
    }

    public void setHeight(Integer height) {
        this.height = height;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", height=" + height +
                '}';
    }

    /**
     * 比较方法，放方法内部定义比较规则
     * 通过该方法的返回值来确定两个对象的比较大小
     * 如果返回值为<0的负整数，表示当前对象小于传入的对象
     * 如果返回值=0，表示两个对象相等
     * 如果返回值为>0的正整数，表示当前对象大于传入的对象
     * @param user
     * @return
     */
    @Override
    public int compareTo(User user) {
        //return this.getId().compareTo(user.getId());
        return this.getId()- user.getId();//升序排序
        //return user.getId()-this.getId();//降序排序
    }

    //根据Id降序排序
    public static class SortIdDesc implements Comparator<User>{

        @Override
        public int compare(User user1, User user2) {
            return user2.getId()-user1.getId();
        }
    }

    //根据年龄升序排序
    public static class SortAgeAsc implements Comparator<User>{

        @Override
        public int compare(User user1, User user2) {
            return user1.getAge()-user2.getAge();
        }
    }

    //根据年龄降序排序
    public static class SortAgeDesc implements Comparator<User>{

        @Override
        public int compare(User user1, User user2) {
            return user2.getAge()-user1.getAge();
        }
    }
}
